package com.dingpeikeji.quickdeer.modules.wx.custom;

import java.io.IOException;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.modules.cupons.entity.CuponsInfo;
import com.dingpeikeji.quickdeer.core.modules.cupons.service.TCuponsService;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.mobile.service.OrderService;
import com.dingpeikeji.quickdeer.modules.base.service.FrontUserService;
import com.dingpeikeji.quickdeer.modules.base.web.FSysController;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.ProRentOrders;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FDemandsService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FRentproductService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.RentproductOrderService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.modules.wx.base.web.WxCommonController;
import com.dingpeikeji.quickdeer.utils.BackResult;
import com.dingpeikeji.quickdeer.utils.StringUtil;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("${wxPath}/custom")
public class WxCustomController {
	@Autowired
	private TCargotypeService tCargotypeService;
	@Autowired
	private FDemandsService demandsService;
	@Autowired
	private TSitedtlService tSitedtlService;
	
	@RequestMapping("customIndex")
	public String customIndex(Model model,HttpServletRequest request) {
		List<TCargotype> cargotypeList = tCargotypeService.getCargotypeListForMobile();
		model.addAttribute("cargotypeList", cargotypeList);
		WxCommonController.defaultTime(model, request);
		return "wx/modules/customProduct/customIndex";
	}
	@RequestMapping("getSiteId")
	public void getSiteId(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String sitename = request.getParameter("sitename");
		String siteid = tSitedtlService.getSiteIdByName(sitename);
		response.getWriter().println(siteid);
	}
	@RequestMapping("demandDetail")
	public String demandDetail(TDemands tDemands,Model model) {
		String demandid = tDemands.getDemandid()+"";
		if (StringUtil.isNotEmpty(demandid)){
			tDemands = demandsService.get(""+tDemands.getDemandid());
			String address = demandsService.getSendAddress(demandid);
			if (StringUtils.isEmpty(address)) {
				address = "";
			}
			tDemands.setSendAddress(address);
			//处理时间
			String formatDateTime = DateUtils.formatDateTime(tDemands.getFromdate());
			tDemands.setFromdateStr(formatDateTime);
			model.addAttribute("tDemands", tDemands);
			return "wx/modules/customProduct/customDetail";
		}
		return "wx/modules/customProduct/customDetail";
	}
	@RequestMapping("customReact")
	public String customReact(Model model,TDemands tDemands,String demandid) {
		tDemands = demandsService.get(""+tDemands.getDemandid());
		String address = demandsService.getSendAddress(demandid);
		tDemands.setSendAddress(address);
		List<DemandsReact> reactList = demandsService.findDemandsReactList(tDemands);
		model.addAttribute("reactList", reactList);
		model.addAttribute("tDemands", tDemands);
		return "wx/modules/customProduct/customReact";
	}
	@RequestMapping(value = "booking")
	public String book( Model model,HttpServletRequest request) throws Exception {
		return "wx/modules/customProduct/customBooking";
	}
}
